package com.lhn.spring5.dao;

import com.lhn.spring5.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * 测试类
 *
 * @author lhn
 * @date 2022/01/05 16:31
 * @since 1.0.0
 **/
@Repository
public class BookDaoImpl implements BookDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加
     * */
    @Override
    public void add(Order order) {
        String sql = "insert into `order` values(?,?)";
        int update = jdbcTemplate.update(sql, order.getOrderId(), order.getOrderName());
        System.out.println(update);
    }

    @Override
    public void updateOrder(Order order) {
        String sql = "update `order` set order_name = ? where order_id = ?";
        System.out.println(jdbcTemplate.update(sql, order.getOrderName(), order.getOrderId()));
    }

    @Override
    public void deleteOrder(int id) {
        String sql = "delete from `order` where order_id = ?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public Integer queryOrder(int id) {
        String sql = "select count(*) from `order`";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    @Override
    public Order findOrder(int id) {
        String sql = "select * from `order` where order_id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper <Order>(Order.class), id);
    }

    @Override
    public List <Order> findAll() {
        String sql = "select * from `order`";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper <Order>(Order.class));
    }

    @Override
    public void batchAdd(List <Object[]> batchArgs) {
        String sql = "insert into `order` values(?,?)";
        System.out.println(Arrays.toString(jdbcTemplate.batchUpdate(sql, batchArgs)));
    }

    @Override
    public void batchUpdate(List <Object[]> batchArgs) {
        String sql = "update `order` set order_name = ? where order_id = ?";
        System.out.println(Arrays.toString(jdbcTemplate.batchUpdate(sql, batchArgs)));
    }

    @Override
    public void batchDelete(List <Object[]> batchArgs) {
        String sql = "delete from `order` where order_id = ?";
        System.out.println(Arrays.toString(jdbcTemplate.batchUpdate(sql, batchArgs)));
    }


}